<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
        xmlns:acl   ="http://www.w3.org/ns/auth/acl#"
        xmlns:rdf   ="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:xsl   ="http://www.w3.org/1999/XSL/Transform"
        xmlns:foaf  = "http://xmlns.com/foaf/0.1/"
        version="1.0">
    <xsl:output   method   = "html"
                  version  = "1.0"
                  encoding = "UTF-8"
                  indent   = "yes" />
    <xsl:key name="nsBindings" match="PrefixBinding" use="@prefix"/>
    <xsl:template match="/">
        <rdf:RDF>
            <xsl:apply-templates select="/ACL"/>
        </rdf:RDF>
    </xsl:template>
    <xsl:template match="ACL">
        <xsl:apply-templates select="User|Service"/>
    </xsl:template>
    <xsl:template match="User">
        <rdf:Description>
            <foaf:name><xsl:value-of select="@name"/></foaf:name>
            <xsl:apply-templates select="AgentClass" mode="user"/>
        </rdf:Description>
    </xsl:template>
    <xsl:template match="Service">
        <xsl:variable name="uri">
            <xsl:call-template name="extractUri"/>
        </xsl:variable>
        <rdf:Description>
            <acl:accessTo rdf:resource="{$uri}"/>
            <xsl:apply-templates select="AnyAgent | Modes/Mode" />
            <xsl:apply-templates select="AgentClass" mode="service"/>
        </rdf:Description>
    </xsl:template>
    <xsl:template match="Mode[not(@type)]">
        <xsl:variable name="uri">
            <xsl:call-template name="extractUri"/>
        </xsl:variable>
        <acl:mode rdf:resource="{$uri}"/>
    </xsl:template>
    <xsl:template match="Mode[@type]">
        <acl:mode rdf:resource="http://www.w3.org/ns/auth/acl#{@type}"/>
    </xsl:template>
    <xsl:template match="AnyAgent">
        <acl:agentClass rdf:resource="http://xmlns.com/foaf/0.1/Agent"/>
    </xsl:template>
    <xsl:template match="AgentClass" mode="service">
        <xsl:variable name="uri">
            <xsl:call-template name="extractUri"/>
        </xsl:variable>
        <acl:agentClass rdf:resource="{$uri}"/>
    </xsl:template>
    <xsl:template match="AgentClass" mode="user">
        <xsl:variable name="uri">
            <xsl:call-template name="extractUri"/>
        </xsl:variable>
        <rdf:type rdf:resource="{$uri}"/>
    </xsl:template>
    <xsl:template name="extractUri">
        <xsl:choose>
            <xsl:when test="@qname">
                <xsl:value-of select="concat(
                        key('nsBindings',substring-before(@qname,':'))/@uri,
                        substring-after(@qname,':'))"
                />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="@uri"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
 </xsl:stylesheet>
